package com.medeiros.upmoney.activity;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medeiros.upmoney.data.LimiteDataProvider;
import com.medeiros.upmoney.model.Categoria;
import com.medeiros.upmoney.model.Limite;

public class DashBoard extends Activity {

	Map<Categoria, Limite> limites;
	List<LimiteView> limiteViews;
	LimiteView limiteGeral;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		try {
			initialize();
		} catch (Exception e) {
			this.finish();
			System.out.println("DashBoard.onCreate() ERROR "+e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			this.reload();
		} catch (IOException e) {
			this.finish();
			System.out.println("DashBoard.onCreate() ERROR "+e.getMessage());
			e.printStackTrace();
		}
	}

	private void reload() throws IOException {
		limites = LimiteDataProvider.getInstance().read(this);
		for (LimiteView limiteView : limiteViews) {
			limiteView.setLimite(limites.get(limiteView.getLimite().getCategoria()));
			limiteView.update();
		}
		limiteGeral.setLimite(getLimiteGeral(limites.values()));
		limiteGeral.update();
	}

	private void initialize() throws Exception {
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);

		limiteViews = new ArrayList<LimiteView>();
		limites = LimiteDataProvider.getInstance().read(this);
		
		limiteGeral = addLimiteView(ll, getLimiteGeral(limites.values()));
		for (Limite limite : limites.values()) {
			this.limiteViews.add(addLimiteView(ll, limite));
		}
		setContentView(ll);
	}

	private LimiteView addLimiteView(LinearLayout ll, Limite limite) {
		LimiteView pb = buildProgressBar(limite);
		TextView tv = new TextView(this);
		tv.setText(limite.getCategoria().getNome());
		ll.addView(tv);
		ll.addView(pb);
		return pb;
	}

	private Limite getLimiteGeral(Collection<Limite> limites) {
		Limite limiteGeral = new Limite();;
		float valor = 0;
		for (Limite limite : limites) {
			limiteGeral.addGasto(limite.getGasto().floatValue());
			valor += limite.getValor().floatValue();
		}
		limiteGeral.setCategoria(Categoria.GERAL);
		limiteGeral.setValor(new BigDecimal(valor));
		return limiteGeral;
	}

	private LimiteView buildProgressBar(Limite limite) {
		LimiteView pb =  new LimiteView(limite, this, null, android.R.attr.progressBarStyleHorizontal);

//		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(500,50);
//		pb.setLayoutParams(params);
		pb.update();
		pb.setOnLongClickListener(new OnLongClickListener() {

			public boolean onLongClick(View v) {
				Intent intent = new Intent(DashBoard.this,  ConfiguracaoLimite.class);
				LimiteView limit = (LimiteView) v;
				intent.putExtra("limite", limit.getLimite());
				startActivity(intent);
				return false;
			}
		});

		pb.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
//				Intent intent = new Intent(DashBoard.this, TecladoGastos.class);
//				LimiteView limit = (LimiteView) v;
//				intent.putExtra("limite", limit.getLimite());
//				startActivity(intent);

				Intent intent = new Intent(DashBoard.this, TecladoGastos.class);
				LimiteView limit = (LimiteView) v;
				intent.putExtra("limite", limit.getLimite());
				startActivity(intent);
			}
		});
		return pb;
	}
}
